<?php
/**
 * 候选人才
 */

namespace app\v1_0\controller\company;

use app\common\model\BaseModel;
use app\common\model\CompanyStaffManage;
use app\common\model\Member;
use app\common\model\Resume;
use app\common\model\ResumeEducation;
use Think\Db;

class ResumeCandidate extends \app\v1_0\controller\common\Base {

    public function _initialize() {
        parent::_initialize();
        $this->checkLogin(1);
        $this->interceptCompanyProfile();
        $this->interceptCompanyAuth();
    }

    public function index() {
        $education = input('post.edu/d', 0, 'intval');
        $experience = input('post.exp/d', 0, 'intval');
        $district = input('post.district/a', []);
        $jobcategory = input('post.jobcategory/a', []);
        $edu_nature = input('post.edu_nature/a',[]);
        $sex = input('post.sex/d', 0, 'intval'); // 性别
        $age = input('post.age/d', 0, 'intval'); // 年龄
        $tag = input('post.tag/a', []);
        $trade = input('post.trade/d', 0, 'intval');
        $major = input('post.major/d', 0, 'intval');
        $nature = input('post.nature/d', 0, 'intval');
        $minwage = input('post.minwage/d', 0, 'intval');
        $maxwage = input('post.maxwage/d', 0, 'intval');
        $settr = input('post.settr/d', 0, 'intval');
        $current_page = input('post.page/d', 1, 'intval');
        $pagesize = input('post.pagesize/d', 10, 'intval');
        $departmentIds = input('post.department_ids/d', []);
        $jobIds = input('post.job_ids', 0);
        $status = input('post.status/d', '');

        $where['a.company_uid'] = $this->userinfo->uid;
        $where['rc.company_id'] = $this->company_profile['id'];
        if ($education > 0) {//学历
            $where['r.education'] = ['=', $education];
        }
        if ($experience > 0) {
            switch ($experience) {
                case 1: //无经验/应届生
                    $where['r.enter_job_time'] = ['=', 0];
                    break;
                case 2://1年以下
                    $where['r.enter_job_time'] = ['>', strtotime('-1 year')];
                    break;
                case 3://1年-2年
                    $where['r.enter_job_time'] = [['<=', strtotime('-1 year')], ['>', strtotime('-2 year')], 'and'];
                    break;
                case 4://2年-3年
                    $where['r.enter_job_time'] = [['<=', strtotime('-2 year')], ['>', strtotime('-3 year')], 'and'];
                    break;
                case 5://3年-5年
                    $where['r.enter_job_time'] = [['<=', strtotime('-3 year')], ['>', strtotime('-5 year')], 'and'];
                    break;
                case 6://5年-10年
                    $where['r.enter_job_time'] = [['<=', strtotime('-5 year')], ['>', strtotime('-10 year')], 'and'];
                    break;
                case 7://10年以上
                    $where['r.enter_job_time'] = [['<', strtotime('-10 year')], ['<>', 0], 'and'];
                    break;
                default:
                    break;
            }
        }
        // 学历性质
        if($edu_nature){
            $uids = Member::where('company_id',$this->company_profile->id)->column('uid');
            $education = ResumeEducation::whereIn('uid',$uids)
                ->when(in_array(2,$edu_nature),function($q){
                    $q->whereIn('school',model(BaseModel::class)->school_985);
                })
                ->when(in_array(3,$edu_nature),function($q){
                    $q->whereIn('school',model(BaseModel::class)->school_211);
                })
                ->field('rid,starttime,endtime')->select();
            $rids = [];
            foreach ($education as $value){
                if(in_array(1,$edu_nature)){
                    $starttime = date('Y',$value['starttime']);
                    $endtime = date('Y',$value['endtime']);
                    if($endtime - $starttime >= 4){
                        $rids[] = $value['rid'];
                    }
                }else{
                    $rids[] = $value['rid'];
                }
            }
            $rids && $where['a.resume_id'] = ['in',$rids];
        }
        $join[] = ['resume r', 'a.resume_id=r.id', 'LEFT'];
        if (!empty($district) || !empty($jobcategory) || !empty($trade) || !empty($nature) || !empty($minwage)) {
            $join[] = ['ResumeIntention ris', 'ris.rid=r.id', 'LEFT'];
            if (!empty($district)) {//意向地区
                if (isset($district[0]) && !empty($district[0])) {
                    $where['ris.district1'] = $district[0];
                }
                if (isset($district[1]) && !empty($district[1])) {
                    $where['ris.district2'] = $district[1];
                }
                if (isset($district[2]) && !empty($district[2])) {
                    $where['ris.district3'] = $district[2];
                }
            }
            if (!empty($jobcategory)) {//意向职位
                if (isset($jobcategory[0]) && !empty($jobcategory[0])) {
                    $where['ris.category1'] = $jobcategory[0];
                }
                if (isset($jobcategory[1]) && !empty($jobcategory[1])) {
                    $where['ris.category2'] = $jobcategory[1];
                }
                if (isset($jobcategory[2]) && !empty($jobcategory[2])) {
                    $where['ris.category3'] = $jobcategory[2];
                }
            }
            if (!empty($trade)) {
                $where['ris.trade'] = $trade;
            }
            if (!empty($nature)) {
                $where['ris.nature'] = $nature;
            }
            if (!empty($minwage)) {
                $where['ris.minwage'] = ['egt', $minwage];
            }
            if (!empty($maxwage)) {
                $where['ris.maxwage'] = ['elt', $maxwage];
            }
        }

        // 性别筛选条件
        if (!empty($sex)) {//审核状态
            $where['r.sex'] = ['=', $sex];
        }

        /**
         * 年龄
         * 参数:0-不限|1-16~20岁|2-20~30岁|3-30~40岁|4-40~50岁|5-50岁以上
         */
        if (!empty($age)) {
            switch ($age) {
                case 1:
                    $where['r.birthday'] = [
                        ['<=', (date('Y') - 16)],
                        ['>=', (date('Y') - 20)]
                    ];
                    break;

                case 2:
                    $where['r.birthday'] = [
                        ['<=', (date('Y') - 20)],
                        ['>=', (date('Y') - 30)]
                    ];
                    break;

                case 3:
                    $where['r.birthday'] = [
                        ['<=', (date('Y') - 30)],
                        ['>=', (date('Y') - 40)]
                    ];
                    break;

                case 4:
                    $where['r.birthday'] = [
                        ['<=', (date('Y') - 40)],
                        ['>=', (date('Y') - 50)]
                    ];
                    break;

                case 5:
                    $where['r.birthday'] = ['<=', (date('Y') - 50)];
                    break;

                default:
                    break;
            }
        }

        if ($tag) {
            $tag_arr = is_array($tag)
                ? $tag
                : explode('_', $tag);
            foreach ($tag_arr as $key => $value) {
                $where[] = ['exp', Db::raw("FIND_IN_SET('{$value}',r.tag)")];
            }
        }

        if (!empty($major)) {//专业
            $where['r.major'] = ['=', $major];
        }

        if (!empty($settr)) {
            $where['r.updatetime'] = ['egt', strtotime('-' . $settr . 'day')];
        }
        //echo count($departmentIds);
        if (!empty($departmentIds)) {
            $where['rc.department_id'] = ['in',$departmentIds];
        }

        if (!empty($jobIds)) {
            $where['a.jobid'] = $jobIds;
        }

        if (isset($status) && is_numeric($status)) {
            $where['rc.status'] = ['=', $status];
        }

        $join[] = ['resume_remark rr', 'a.resume_id=rr.resume_id and a.comid=rr.comid', 'LEFT'];
        $join[] = ['member m', 'a.personal_uid=m.uid', 'RIGHT'];
        $join[] = ['ResumeCandidate rc', 'rc.resume_id=r.id', 'INNER'];

        $total = model('JobApply')->alias('a');
        foreach ($join as $k => $v) {
            $total = $total->join($v[0], $v[1], $v[2]);
        }
        $total = $total->where($where)
            ->field('r.id')
            ->group('r.id')
            ->count();

        $list = model('JobApply')->alias('a');
        foreach ($join as $k => $v) {
            $list = $list->join($v[0], $v[1], $v[2]);
        }
        $list = $list->field('a.id,a.comid,a.companyname,a.jobid,a.jobname,a.resume_id,a.fullname,a.note,a.addtime,
        a.is_look,a.handle_status,a.source,a.platform,r.display_name,r.high_quality,r.birthday,r.sex,r.education,r.enter_job_time,
        r.photo_img,r.current,r.updatetime,IFNULL(rr.remark,"") as remark,r.audit,r.uid as resume_uid,r.tag,r.download_time,
        rc.recruit_remark,rc.command_id,rc.interview_time')
            ->where($where)
            ->where('r.id', 'not null')
            ->order('r.download_time desc')
            ->order('a.id desc')
            ->page($current_page, $pagesize)
            ->select();
        $resumeid_arr = [];
        $intention_arr = [];
        $work_arr = [];
        $education_arr = [];
        $photo_id_arr = [];
        $photo_data = [];
        $fullname_arr = [];
        foreach ($list as $key => $value) {
            $resumeid_arr[] = $value['resume_id'];
            $value['photo_img'] > 0 && ($photo_id_arr[] = $value['photo_img']);
        }
        if (!empty($photo_id_arr)) {
            $photo_data = model('Uploadfile')->getFileUrlBatch(
                $photo_id_arr
            );
        }
        if (!empty($resumeid_arr)) {
            $intention_data = model('ResumeIntention')
                ->where('rid', 'in', $resumeid_arr)
                ->order('id asc')
                ->select();
            foreach ($intention_data as $key => $value) {
                $intention_arr[$value['rid']][] = $value;
            }
            $work_data = model('ResumeWork')
                ->where('rid', 'in', $resumeid_arr)
                ->order('id desc')
                ->select();
            foreach ($work_data as $key => $value) {
                $work_arr[$value['rid']][] = $value;
            }
            $education_data = model('ResumeEducation')
                ->where('rid', 'in', $resumeid_arr)
                ->order('id desc')
                ->select();
            foreach ($education_data as $key => $value) {
                $education_arr[$value['rid']] = $value;
            }
            $fullname_arr = model('Resume')->formatFullname($resumeid_arr, $this->userinfo);
        }
        $category_data = model('Category')->getCache();
        $category_job_data = model('CategoryJob')->getCache();
        $category_district_data = model('CategoryDistrict')->getCache();
        foreach ($list as $key => $value) {
            $value['interview_time'] && $value['interview_time'] = date('Y-m-d H:i',$value['interview_time']);
            $value['fullname'] = isset($fullname_arr[$value['resume_id']]) ? $fullname_arr[$value['resume_id']] : $value['fullname'];
            $value['sex_text'] = model('Resume')->map_sex[$value['sex']];
            $value['education_text'] = isset(
                model('BaseModel')->map_education[$value['education']]
            )
                ? model('BaseModel')->map_education[$value['education']]
                : '';
            $value['education'] = $education_arr[$value['resume_id']] ?? [];
            $value['experience_text'] =
                $value['enter_job_time'] == 0
                    ? '无经验'
                    : format_date($value['enter_job_time']) . '经验';
            $value['current_text'] = isset(
                $category_data['QS_current'][$value['current']]
            )
                ? $category_data['QS_current'][$value['current']]
                : '';
            if (isset($work_arr[$value['resume_id']])) {
                $value['work_list'] = array_slice($work_arr[$value['resume_id']], 0, 3);
            } else {
                $value['work_list'] = [];
            }

            $value['age'] = date('Y') - intval($value['birthday']);
            $district_arr = $category_arr = [];
            if (isset($intention_arr[$value['resume_id']])) {
                foreach ($intention_arr[$value['resume_id']] as $k => $v) {
                    if ($v['trade']) {
                        $trade_arr[] =
                            $category_data['QS_trade'][$v['trade']];
                    }
                    if ($v['nature']) {
                        $nature_arr[] = model('Resume')->map_nature[$v['nature']];
                    }
                    $wage_arr[0] = $v['minwage'] . '-' . $v['maxwage'];
                    if ($v['category']) {
                        $category_arr[] = isset(
                            $category_job_data[$v['category']]
                        )
                            ? $category_job_data[$v['category']]
                            : '';
                    }
                    if ($v['district']) {
                        $district_arr[] = isset(
                            $category_district_data[$v['district']]
                        )
                            ? $category_district_data[$v['district']]
                            : '';
                    }
                }
            }
            if (!empty($trade_arr)) {
                $trade_arr = array_unique($trade_arr);
                $value['intention_trade'] = implode(',', $trade_arr);
            } else {
                $value['intention_trade'] = '';
            }
            if (!empty($category_arr)) {
                $category_arr = array_unique($category_arr);
                $value['intention_jobs'] = implode(',', $category_arr);
            } else {
                $value['intention_jobs'] = '';
            }
            if (!empty($wage_arr)) {
                $wage_arr = array_unique($wage_arr);
                $value['intention_wage'] = implode(',', $wage_arr);
            } else {
                $value['intention_wage'] = '';
            }
            if (!empty($district_arr)) {
                $district_arr = array_unique($district_arr);
                $value['intention_district'] = implode(',', $district_arr);
            } else {
                $value['intention_district'] = '';
            }
            if (!empty($nature_arr)) {
                $nature_arr = array_unique($nature_arr);
                $value['intention_nature'] = implode(',', $nature_arr);
            } else {
                $value['intention_nature'] = '';
            }
            $value['photo_img_src'] = isset(
                $photo_data[$value['photo_img']]
            )
                ? $photo_data[$value['photo_img']]
                : default_empty('photo', $value['sex']);

            $value['resume_link_url_web'] = url('index/resume/show', ['id' => $value['resume_id']], true, $this->sub_site_domain);
            $value['job_link_url_web'] = url('index/job/show', ['id' => $value['jobid']], true, $this->sub_site_domain);

            /**
             * 联系方式
             */
//            $resumeinfo = [
//                'id' => $value['resume_id'],
//                'uid' => $value['resume_uid']
//            ];
//            $getResumeContact = model('Resume')->getContact($resumeinfo, $this->userinfo);
//            if ($getResumeContact['show_contact'] == 1) {
            $resume_mobile = model('Member')->where('uid', $value['resume_uid'])->value('mobile');
            $value['resume_contact'] = $resume_mobile;
//            } else {
//                $value['resume_contact'] = 0;
//            }

            if ($value['tag']) {
                $tagTextArr = [];
                $tagArr = explode(',', $value['tag']);
//                foreach ($tagArr as $tagId) {
//                    $tagTextArr[] = $tagId;
//                }
                $value['tag'] = $tagArr;
            }


            $list[$key] = $value;
        }

        $return['items'] = $list;
        $return['total'] = $total;
        $return['current_page'] = $current_page;
        $return['pagesize'] = $pagesize;
        $return['total_page'] = ceil($total / $pagesize);
        $return['statistics_status'] = $this->statisticsStatus($where,$join);
        $this->ajaxReturn(200, '获取数据成功', $return);
    }
    private function statisticsStatus($where,$join){
        unset($where['rc.status']);
//        $where = [
//            'a.company_uid' => $this->userinfo->uid,
//            'rc.company_id' => $this->company_profile['id']
//        ];
            $query = model('JobApply')->alias('a');
        foreach ($join as $k => $v) {
            $query = $query->join($v[0], $v[1], $v[2]);
        }
        $list = $query
            ->field('rc.status,count(*) as num')
            ->where($where)
            ->group('rc.status')
            ->select();
        if (false === $list) {
            $this->ajaxReturn(500, model('JobApply')->getError());
        }
        $result = [];
        foreach ($list as $item) {
            $result[$item['status']] = $item['num'];
        }
        return $result;
    }

    //添加候选人
    public function add() {
        $companyId = $this->company_profile['id'];
        $resumeIds = input('post.ids/a', []);
        $status = input('post.status/d', 0, 'intval');

        $existIds = model('ResumeCandidate')
            ->where('company_id', $companyId)
            ->whereIn('resume_id', $resumeIds)
            ->column('resume_id');
        $resumeIds = array_diff($resumeIds, $existIds);
        if (empty($resumeIds)) {
            $this->ajaxReturn(500, '候选人已存在');
        }

        $dataList = [];
        foreach ($resumeIds as $resumeId) {
            $dataList[] = [
                'company_id' => $companyId,
                'resume_id' => $resumeId,
                'status' => $status,
            ];
        }
        $result = model('ResumeCandidate')
            ->validate(true)
            ->allowField(true)
            ->saveAll($dataList);
        if (false === $result) {
            $this->ajaxReturn(500, model('ResumeCandidate')->getError());
        }
        $this->ajaxReturn(200, '添加成功');
    }

    //更新状态
    public function update() {
        //面试ID
        $content = input('post.content/a');
        $where = [
            'company_id' => $this->company_profile['id'],
            'resume_id' => input('post.resume_id/d', 0, 'intval'),
        ];
        if (!$res = \app\common\model\ResumeCandidate::where($where)->field('id,command_id')->find()) {
            return $this->returnJson(500, 'resume_id参数有误');
        }
        $data = [
            'status' => input('post.status/d', 0, 'intval'),
            'interview_time' => !empty($content['mtime'])?strtotime($content['mtime']):0,
            'recruit_remark' => '',
        ];
        !empty($content['uid']) && $data['recruit_uid'] = $content['uid'];
        !empty($content['command_id']) && $data['command_id'] = $content['command_id'];
        if($data['status'] == 5){
            $command_id_to_status = [1=>5,2=>6,3=>7,4=>8];
            $data['status'] = $command_id_to_status[$res['command_id']]??5;
        }
        $result = model('ResumeCandidate')
            ->validate(true)
            ->allowField(true)
            ->save($data, $where);
        if (false === $result) {
            $this->ajaxReturn(500, model('ResumeCandidate')->getError());
        }

        if($data['status'] == 12){// 已入职  在员工表添加数据
            CompanyStaffManage::add(['resume_id'=>$where['resume_id'],'company_id'=>$where['company_id']]);
        }
        $this->ajaxReturn(200, '操作成功');
    }
    //批量更新状态
    public function updateBatch() {
        //面试ID
        $content = input('post.content/a');
        if(!$ids = input('ids/a',[])){
            return $this->returnJson(500,'请选择候选人');
        }
        $status = input('post.status/d', 0, 'intval');
        $where = [
            'company_id' => $this->company_profile['id'],
            'resume_id' => ['in',$ids],
        ];
        $res = \app\common\model\ResumeCandidate::where($where)->field('id,command_id')->select();
        $data = [
            'status' => $status,
//            'command_id' => $content['command_id']??0,
            'interview_time' => !empty($content['mtime'])?strtotime($content['mtime']):0,
        ];
        !empty($content['uid']) && $data['recruit_uid'] = $content['uid'];
        !empty($content['command_id']) && $data['command_id'] = $content['command_id'];
        $command_id_to_status = [1=>5,2=>6,3=>7,4=>8];
        foreach ($res as $value){
            if($status == 5){
                $data['status'] = $command_id_to_status[$value['command_id']]??5;
            }
            $update[] = [
                'id'=>$value['id'],
            ] + $data;
        }

//        $result = model('ResumeCandidate')
//            ->validate(true)
//            ->allowField(true)
//            ->save($data, $where);
        $result = model(\app\common\model\ResumeCandidate::class)->updateBatch($update??[]);
        if (false === $result) {
            $this->ajaxReturn(500, model('ResumeCandidate')->getError());
        }
        if($data['status'] == 12){// 已入职  在员工表添加数据
            CompanyStaffManage::addBatch(['resume_ids'=>$ids,'company_id'=>$this->company_profile['id']]);
        }
        $this->ajaxReturn(200, '操作成功');
    }

    //删除候选人
    public function delete() {
        $where = [
            'company_id' => $this->company_profile['id'],
            'resume_id' => input('post.resume_id/d', 0, 'intval'),
        ];
        $result = model('ResumeCandidate')
            ->validate(true)
            ->where($where)
            ->delete();
        if (false === $result) {
            $this->ajaxReturn(500, model('ResumeCandidate')->getError());
        }
        $this->ajaxReturn(200, '删除成功');
    }
    //批量删除候选人
    public function deleteBatch() {

        if(!$ids = input('ids/a',[])){
            return $this->returnJson(500,'请选择候选人');
        }
        $where = [
            'company_id' => $this->company_profile['id'],
            'resume_id' => ['in',$ids],
        ];
        $result = model('ResumeCandidate')
            ->validate(true)
            ->where($where)
            ->delete();
        if (false === $result) {
            $this->ajaxReturn(500, model('ResumeCandidate')->getError());
        }
        $this->ajaxReturn(200, '删除成功');
    }

    public function toDepartment() {
        $where = [
            'company_id' => $this->company_profile['id'],
            'resume_id' => input('post.resume_id/d', 0, 'intval'),
        ];
        //print_r($where);die;
        $data = [
            'department_id' => input('post.department_id/d', 0, 'intval'),
            'status'=>1,
        ];
        $result = model('ResumeCandidate')
            ->validate(true)
            ->allowField(true)
            ->save($data, $where);
        if (false === $result) {
            $this->ajaxReturn(500, model('ResumeCandidate')->getError());
        }
        $this->ajaxReturn(200, '操作成功');
    }
    public function toDepartmentBatch() {
        if(!$ids = input('ids/a',[])){
            return $this->returnJson(500,'请选择候选人');
        }
        if(!$department_id = input('post.department_id/d', 0, 'intval')){
            return $this->returnJson(500,'请选择部门');
        }
        $where = [
            'company_id' => $this->company_profile['id'],
            'resume_id' => ['in',$ids],
        ];
        $data = [
            'department_id' => $department_id,
            'status'=>1,
        ];
        $result = model('ResumeCandidate')
            ->validate(true)
            ->allowField(true)
            ->save($data, $where);
        if (false === $result) {
            $this->ajaxReturn(500, model('ResumeCandidate')->getError());
        }
        $this->ajaxReturn(200, '操作成功');
    }

    public function updateJob() {
        $where = [
            'company_uid' => $this->userinfo->uid,
            'id' => input('post.id/d', 0, 'intval'),
        ];
        $jobId = input('post.job_id/d', 0, 'intval');
        $jobName = model('Job')->where('id', $jobId)->where('company_id', $this->company_profile['id'])->value('jobname');
        if (!$jobName) {
            $this->ajaxReturn(500, '职位错误');
        }
        $data = [
            'jobid' => $jobId,
            'jobname' => $jobName
        ];
        $result = model('JobApply')->where($where)->update($data);
        if (false === $result) {
            $this->ajaxReturn(500, model('JobApply')->getError());
        }
        $this->ajaxReturn(200, '操作成功');
    }
    public function updateJobBatch() {
        $where = [
            'company_uid' => $this->userinfo->uid,
            'id' => ['in',input('post.ids/a')],
        ];
        $jobId = input('post.job_id/d', 0, 'intval');
        $jobName = model('Job')->where('id', $jobId)->where('company_id', $this->company_profile['id'])->value('jobname');
        if (!$jobName) {
            $this->ajaxReturn(500, '职位错误');
        }
        $data = [
            'jobid' => $jobId,
            'jobname' => $jobName
        ];
        $result = model('JobApply')->where($where)->update($data);
        if (false === $result) {
            $this->ajaxReturn(500, model('JobApply')->getError());
        }
        $this->ajaxReturn(200, '操作成功');
    }

    public function updateTags() {
        $where = [
            'id' => input('post.resume_id/d', 0, 'intval')
        ];
        if(!$tagIds = input('post.tag_ids/a', [])){
            return $this->returnJson(500,'请选择标签');
        }

        $tags  = model('Resume')->where($where)->value('tag');
        $tags = explode(',',$tags);
        $data['tag'] = implode(',',array_filter(array_unique(array_merge($tags,$tagIds))));
        $result = model('Resume')->where($where)->update($data);
        if (false === $result) {
            $this->ajaxReturn(500, model('Resume')->getError());
        }
        $this->ajaxReturn(200, '操作成功');
    }
    public function closeTag() {
        $where = [
            'id' => input('post.resume_id/d', 0, 'intval')
        ];
        if(!$tag = input('post.tag/s', '')){
//            return $this->returnJson(500,'标签参数为空');
        }
        $tags  = model('Resume')->where($where)->value('tag');
        $tags = explode(',',$tags);
        $tags = array_filter($tags,function ($v)use($tag){
            return $v !== $tag;
        });
        $data['tag'] = implode(',',$tags);
        $result = model('Resume')->where($where)->update($data);
        if (false === $result) {
            $this->ajaxReturn(500, model('Resume')->getError());
        }
        $this->ajaxReturn(200, '操作成功');
    }
    public function updateTagsBatch() {
        $resume_ids = input('post.resume_id/a');
        $where = [
            'id' => ['in',$resume_ids],
        ];
        if(!$tagIds = input('post.tag_ids/a', [])){
            return $this->returnJson(500,'请选择标签');
        }
        $res  = model('Resume')->where($where)->column('tag','id');
        $update = [];
        foreach ($resume_ids as $v){
            $tag = $res[$v]??'';
            $tag = explode(',',$tag);
            $update[] = [
                'id'=>$v,
                'tag'=>implode(',',array_filter(array_unique(array_merge($tag,$tagIds))))
            ];
        }
        $result = model('Resume')->updateBatch($update);
        if (false === $result) {
            $this->ajaxReturn(500, model('Resume')->getError());
        }
        $this->ajaxReturn(200, '操作成功');
    }

    public function getDepartmentTree() {
        $where = ['com_id' => $this->company_profile['id']];
        $list = model('CompanyStructure')
            ->field("id as `value`, `name` as `label`,p_id")
            ->where('p_id','>','0')
            ->where($where)
            ->select();
        foreach ($list as $key => $item) {
            $list[$key] = $item->toArray();
        }
        $tree = $this->generateTree($list, 'value');
        $tree = $this->resetKeys($tree);
        $this->ajaxReturn(200, '获取到数据', $tree);
    }

    public function getStatusCount() {
        $where = [
            'a.company_uid' => $this->userinfo->uid,
            'rc.company_id' => $this->company_profile['id']
        ];
        $list = model('JobApply')->alias('a')
            ->field('rc.status,count(*) as num')
            ->join('member m', 'm.uid = a.personal_uid','right')
            ->join('ResumeCandidate rc', 'a.resume_id = rc.resume_id')
            ->where($where)
            ->group('rc.status')
            ->select();
        if (false === $list) {
            $this->ajaxReturn(500, model('JobApply')->getError());
        }
        $result = [];
        foreach ($list as $item) {
            $result[$item['status']] = $item['num'];
        }
        $this->ajaxReturn(200, '操作成功', $result);
    }

}
